package Exercise;

class Students{
    private int id;  //学号
    private String name;  //姓名
    private int age;  //年龄

    public Students() {
    }

    public Students(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
public class Dui_Arrays {
    public static void main(String[] args){
        //生成对象数组和对象
        Students[] arr = new Students[3];
        Students s1 = new Students(1,"李四",12);
        Students s2 = new Students(2,"王五",14);
        //添加数组初始数据
        arr[0] = s1;
        arr[1] = s2;
        //添加新的学生对象
        Students s3 = new Students(4,"张三",13);
        //学号不唯一，提示错误
        if(Judge(arr,s3.getId())){
            System.out.println("学号不唯一，无法添加");
        }
        //学号唯一，进行添加
        else{
            //计数器
            int count = 0;
            count = Count(arr);
            //数组大小足够
            if(count<arr.length){
                //添加学生信息
                arr[count] = s3;
                Print(arr);
                //删除学生信息
                int index = Index(arr,1);
                arr = Detele(arr,index);
                Print(arr);
            }
            //数组大小不足
            else{
                //生成一个新数组，长度为旧数组的长度加1，并将内容复制到新数组中
                Students[] newarr = new Students[arr.length+1];
                newarr = Copy(newarr,arr);
                //添加学生信息
                newarr[count] = s3;
                Print(newarr);
                //删除学生信息
                int index = Index(newarr,2);
                newarr = Detele(newarr,index);
                Print(arr);
            }
        }
    }

    //学号唯一性检验
    public static boolean Judge(Students[] arr,int id){
        for(int i = 0;i<=arr.length-1;i++) {
            if (arr[i] != null) {
                if (arr[i].getId() == id)
                    return true;
            }
        }
        return false;
    }
    //统计数组中元素个数
    public static int Count(Students[] arr){
        int count =0;
        for (int i = 0; i <= arr.length-1; i++) {
            //数组内容不为空
            if(arr[i]!=null){
                count++;
            }
        }
        return count;
    }
    //复制原数组内容
    public static Students[] Copy(Students[] newarr,Students[] arr){
        for (int i = 0; i <= arr.length-1; i++) {
            newarr[i] = arr[i];
        }
        return newarr;
    }
    //遍历数组
    public static void Print(Students[] arr){
        for (int i = 0; i <=arr.length-1; i++) {
            if(arr[i]!=null)
                System.out.println(arr[i].getId()+arr[i].getName()+arr[i].getAge());
        }
    }
    //获取要删除元素的索引
    public static int Index(Students[] arr,int id){
        for (int i = 0; i <= arr.length-1; i++) {
            if(arr[i]!=null){
                if(arr[i].getId()==id){
                    return i;
                }
            }
        }
        return -1;
    }
    //删除元素
    public static Students[] Detele(Students[] arr, int index){
        //删除元素
        for (int i = index; i <arr.length-1 ; i++) {
            if(index!=-1)
                arr[i] = arr[i+1];
        }
        arr[arr.length-1]= null;
        return arr;
    }
}
